package com.cl.unipay4j.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.cl.unipay4j.core.IPayConfig;
import com.cl.unipay4j.core.Result;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 支付宝网页支付
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class AliPayPc extends AliPay {


    /**
     * 销售产品码，商家和支付宝签约的产品码
     * 目前仅支持FAST_INSTANT_TRADE_PAY
     */
    private static final String PRODUCT_CODE = "FAST_INSTANT_TRADE_PAY";


    public AliPayPc(IPayConfig aliPaySet) {
        super(aliPaySet);
    }

    public AliPayPc() {
        super();
    }


    /**
     * 支付
     *
     * @param  params  支付请求参数
     * @return 支付返回结果
     * @throws AlipayApiException
     */
    @Override
    public Result executePay(Map<String, Object> params) throws AlipayApiException {
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(payConfig.getReturnUrl());
        alipayRequest.setNotifyUrl(payConfig.getNotifyUrl());
        params.put("product_code", PRODUCT_CODE);
        alipayRequest.setBizContent(JSON.toJSONString(params));
        AlipayTradePagePayResponse r = this.alipayClient.pageExecute(alipayRequest);
        if (r.isSuccess()) {
            return Result.returnSuccess(r.getBody());
        } else {
            return Result.returnFail(r.getSubCode(), r.getSubMsg());
        }
    }

    /**
     * 交易类型
     *
     * @return  交易类型
     */
    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.PC;
    }

    @Override
    public boolean needBuyerId() {
        return false;
    }
}
